package org.itboys.weixin;

import com.google.common.collect.Maps;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.fuzion.entity.FuzionMember;
import org.itboys.manager.ProductWeixinService;
import org.itboys.product.entity.Product;
import org.itboys.product.entity.ProductCategory;
import org.itboys.product.entity.ProductText;
import org.itboys.product.service.ProductService;
import org.itboys.product.service.ProductTextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by huml on 2016/4/15.
 * 微信产品控制层
 */
@RestController
@RequestMapping("/weixin/product")
public class ProductWeixinController extends BaseController {
    @Autowired
    private ProductWeixinService productWeixinService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTextService productTextService;

    /**
     * 能量商城首页
     * @param request
     * @param model
     */
    @RequestMapping("/productIndex")
    public ModelAndView productIndex(HttpServletRequest request, Model model){
        Long sellerId = (Long) request.getSession().getAttribute("sellerId");
        if(sellerId == null){
            sellerId = 0L;
        }
        List<ProductCategory> categories = productWeixinService.productIndexData(0L);
        model.addAttribute("data", categories);
        return new ModelAndView("site/productIndex");
    }

    /**
     * 产品详情页
     * @param productId
     * @param model
     */
    @RequestMapping("/productDetail")
    public ModelAndView productDetail(@RequestParam("productId")Long productId,
                              Model model){
        Product product = productService.getById(productId);
        ProductText productText = productTextService.getByField("productId", productId);
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
        model.addAttribute("product", product);
        model.addAttribute("productText", productText);
        return new ModelAndView("site/productDetail");
    }

}
